<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0;">
      <el-form :inline="true" :model="query">
        <el-form-item>
          <el-input v-model="query.name" placeholder="店铺名称"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search">查询</el-button>
        </el-form-item>
      </el-form>
    </el-col>
    <!--列表-->
    <el-table border :data="tableData" highlight-current-row v-loading="listLoading"
              style="width: 100%;">
      <el-table-column prop="name" label="店铺名称">
      </el-table-column>
      <el-table-column prop="tel" label="联系电话">
      </el-table-column>
      <el-table-column prop="address" label="地址">
      </el-table-column>
      <el-table-column prop="logo" label="店铺logo">
        <template slot-scope="scope">
          <!--在图片链接前面同意加上vue全局变量FILE_SERVER-->
          <img :src="FILE_SERVER + scope.row.logo" alt="" style="width: 50px;height: 50px;border-radius: 50%;">
        </template>
      </el-table-column>
      <el-table-column prop="state" label="店铺状态">
        <!--1.待审核 2.审核通过待激活 3.激活成功 4.驳回-->
        <template slot-scope="scope">
          <el-tag v-if="scope.row.state === 1" type="danger">待审核</el-tag>
          <el-tag v-else-if="scope.row.state === 2" type="warning">审核通过待激活</el-tag>
          <el-tag v-else-if="scope.row.state === 3" type="success">激活成功</el-tag>
          <el-tag v-else type="info">驳回</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button :disabled="scope.row.state !== 1 && scope.row.state !== 4" size="small"
                     type="primary" @click="handleEdit(scope.row)">
            审核
          </el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[5, 10, 20, 50]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          style="float:right;">
      </el-pagination>
    </el-col>
    <el-dialog title="审核店铺信息" :visible.sync="dialogVisible" @close="dialogClose" width="30%">
      <el-form :model="shop" label-width="80px" :rules="rules" ref="rules">
        <el-form-item label="店铺名称" prop="name">
          <el-input v-model="shop.name" disabled></el-input>
        </el-form-item>
        <el-form-item label="联系电话" prop="tel">
          <el-input v-model="shop.tel" disabled></el-input>
        </el-form-item>
        <el-form-item label="店铺地址" prop="address">
          <el-input v-model="shop.address" disabled></el-input>
        </el-form-item>
        <el-form-item label="店铺logo" prop="logo">
          <img :src="FILE_SERVER + shop.logo" alt="" style="width: 50px;height: 50px;border-radius: 50%;">
        </el-form-item>
        <!--审核备注-->
        <el-form-item prop="remark" label="审核备注">
          <el-input v-model="shop.remark" type="textarea" :rows="3" placeholder="请输入审核备注"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="danger" @click="changeStatus(4)">驳回</el-button>
          <el-button type="success" @click="changeStatus(2)">通过</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </section>
</template>

<script>
export default {
  name: "Shop",
  data() {
    return {
      remark: '',
      query: {
        name: ''
      },
      currentPage: 1,
      pageSize: 5,
      total: 0,
      tableData: [],
      fileList: [],
      listLoading: false,
      selection: [],
      dialogVisible: false,
      uploadDialog: false,
      title: '',
      token: {
        "TOKEN": localStorage.getItem('token')
      },
      shop: {},
      rules: {
        remark: [
          {required: true, message: '请输入审核备注'}
        ]
      },
      deptList: [],
    }
  },
  mounted() {
    this.getShopList();
  },
  methods: {
    search() {
      this.currentPage = 1;
      this.getShopList();
    },
    getShopList() {
      this.$axios.post('/shop', {
        name: this.query.name,
        page: this.currentPage,
        pageSize: this.pageSize
      }).then(res => {
        if (res.data.code === 1) {
          this.tableData = res.data.data.rows;
          this.total = res.data.data.total;
        } else {
          this.$message.error(res.data.msg);
        }
      })
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.getShopList();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getShopList();
    },
    dialogClose() {
      this.shop = {};
      this.dialogVisible = false;
      this.$refs['rules'].resetFields();
    },
    handleEdit(row) {
      this.dialogVisible = true;
      this.shop = {...row};
    },
    changeStatus(status) {
      // 如果status是4则校验remark有没有填 如果没有填则提示 如果校验通过则发送请求
      // 如果status是2则直接发送请求
      if (status === 4) {
        this.$refs['rules'].validate((valid) => {
          if (valid) {
            this.$axios.put('/shop/audit?' + 'id=' + this.shop.id + '&state=' + status + '&remark=' + this.shop.remark).then(res => {
              if (res.data.code === 1) {
                this.$message.success("操作成功");
                this.dialogClose();
                this.getShopList();
              }
            })
          }
        })
      } else if (status === 2) {
        this.$axios.put('/shop/audit?' + 'id=' + this.shop.id + '&state=' + status + '&remark=' + this.shop.remark).then(res => {
          if (res.data.code === 1) {
            this.$message.success("操作成功");
            this.dialogClose();
            this.getShopList();
          }
        })
      }
    },
    handleDel(id) {
      this.$confirm('确认删除该记录吗？', '提示', {type: 'warning'}).then(() => {
        this.listLoading = true;
        this.$axios.delete('/shop/' + id).then((res) => {
          this.listLoading = false;
          if (res.data.code === 1) {
            this.$message.success("删除成功")
            //如果当前页数据被删除完了，则自动切换到上一页
            if (this.tableData.length === 1 && this.currentPage > 1) {
              this.currentPage--;
            }
            this.getShopList();
          } else {
            this.$message.error(res.data.msg || '删除失败');
          }
        })
      })
    }
  }
}
</script>

<style scoped>

</style>